package com.saint.tvtest.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.saint.tvtest.R;
import com.saint.tvtest.activity.Main2Activity;
import com.saint.tvtest.bean.DataBean;

import java.util.List;

/**
 * Created by Administrator on 2018/2/23.
 */

public class MenuAdapter extends BaseAdapter {
    private List<DataBean> catsBeans;
    private LayoutInflater inflater;
    private Context context;
    private Handler handler;
    private int menuFlag;

    public MenuAdapter(List<DataBean> catsBeans, Context context, Handler handler, int menuFlag) {
        this.catsBeans = catsBeans;
        this.context = context;
        this.handler = handler;
        this.menuFlag = menuFlag;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return catsBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return catsBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return catsBeans.get(i).getId();
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (null == view) {
            view = inflater.inflate(R.layout.item_menu, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        String color = catsBeans.get(i).getName_color_value();
        if (!TextUtils.isEmpty(color)) {
            if (!color.startsWith("#")) {
                color = "#" + color;
            }
            holder.tvTitle.setTextColor(Color.parseColor(color));
        }
        holder.tvTitle.setText(catsBeans.get(i).getName());
        view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                Log.e("OnKeyDown", "view：" + view.getId() + "    boolean: " + b);
            }
        });

        holder.tvTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Message msg = handler.obtainMessage();
                if (menuFlag == 1) {
                    msg.what = Main2Activity.HANDLER_GET_SUB_DATA;
                    msg.arg1 = i;
                    handler.handleMessage(msg);
                } else if (2 == menuFlag) {
                    msg.what = Main2Activity.HANDLER_GET_SUB_CONTENT;
                    msg.arg1 = i;
                    handler.handleMessage(msg);
                }

            }
        });

        return view;
    }

    public static class ViewHolder {
        private ImageView ivBG;
        private TextView tvTitle;

        public ViewHolder(View view) {
            this.ivBG = view.findViewById(R.id.item_iv_bg);
            this.tvTitle = view.findViewById(R.id.item_tv_title);
        }
    }
}
